﻿<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
    CodeBehind="Relations.aspx.cs" Inherits="DFT.Relations" %>

<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <table width="100%">
        <tr>
            <td align="right" valign="top" style="width: 200px;">
                الأقارب الموجودين:
                <ul id="relations">
                </ul>
            </td>
            <td align="right" valign="top">
                <div class="ui-widget">
                    <label for="trees">
                        ابحث عن شجرة:
                    </label>
                    <input id="trees" />
                    <input runat="server" type="button" id="addTree" visible="false" value="اضافة لقائمة الأقارب"
                        class="button gray" />
                </div>
                <asp:Label ID="treeName" runat="server"></asp:Label>
                <asp:TreeView ID="familyTreeView" runat="server" ShowLines="false">
                </asp:TreeView>
            </td>
        </tr>
    </table>
    <script language="javascript" type="text/javascript">
        function ShowRelation(id, name) {
            document.location.href = "Relations.aspx?ids=" + id + "&name=" + name;
        }

        function AddRelation(id, name) {
            $.ajax({
                url: "AJAX/AddRelatuionUser.aspx?id=" + id + "&name=" + name,
                cache: false,
                async: false,
                success: function (msg) {
                    ShowRelation(id, name);
                }
            });
        }

        function RemoveRelation(id) {
            $.ajax({
                url: "AJAX/RemoveRelatuionUser.aspx?id=" + id,
                cache: false,
                async: false,
                success: function (msg) {
                    $("#rel_" + id).remove();
                }
            });
        }

        $(function () {
            $.ajax({
                url: "AJAX/GetAllRelatuionsUser.aspx",
                cache: false,
                dataType: "xml",
                async: false,
                success: function (xml) {
                    $(xml).find("Relation").each(function () {
                        $("#relations").append('<li id=rel_' + $(this).find("Id").text() + '>' + $(this).find("Name").text() +
                        '<a herf=# onclick="ShowRelation(' + $(this).find("TreeId").text() + ',' + "'" + $(this).find("Name").text() + "'" + ');"> عرض </a>' +
                        '<a herf=# onclick="RemoveRelation(' + $(this).find("Id").text() + ');"> حذف </a></li>');
                    });
                }
            });
            $.ajax({
                url: "AJAX/GetAllTrees.aspx",
                cache: false,
                dataType: "xml",
                success: function (xml) {
                    var data = $("Tree", xml).map(function () {
                        return {
                            value: $("Name", this).text(),
                            id: $("Id", this).text()
                        };
                    }).get();
                    $("#trees").autocomplete({
                        source: data,
                        minLength: 0,
                        select: function (event, ui) {
                            if (ui.item) {
                                document.location.href = "Relations.aspx?ida=" + ui.item.id + "&name=" + ui.item.value;
                            }
                        }
                    });
                }
            });
        });
    </script>
</asp:Content>
